package com.xiaobias.a05;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;

/*
    BeanFactory 后置处理器的作用
 */
public class A5Application {

    private static final Logger logger = LoggerFactory.getLogger(A5Application.class);

    public static void main(String[] args) throws Exception {
        // GenericApplicationContext 是一个干净的容器
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean(Config.class);
        context.registerBean(ComponentScanPostProcessor.class);
//        // @ComponentsScan @Bean @Import @ImportResource
//        context.registerBean(ConfigurationClassPostProcessor.class);
        // @MapperScanner
//        context.registerBean(MapperScannerConfigurer.class, bd -> {
//            bd.getPropertyValues().add("basePackage", "com.xiaobias.a05.mapper");
//        });

//
        context.registerBean(AtBeanPostProcessor.class);
//        CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
//        // 不走类加载
//        MetadataReader reader = factory.getMetadataReader(new ClassPathResource("com/xiaobias/a05/Config.class"));
//        Set<MethodMetadata> methods = reader.getAnnotationMetadata().getAnnotatedMethods(Bean.class.getName());
//        for (MethodMetadata method : methods) {
//            System.out.println(method);
//            String iniMethod = method.getAnnotationAttributes(Bean.class.getName()).get("initMethod").toString();
//            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
//            builder.setFactoryMethodOnBean(method.getMethodName(), Config.class.getName());
//            builder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR);
//            if (!iniMethod.isEmpty()) {
//                builder.setInitMethodName(iniMethod);
//            }
//            AbstractBeanDefinition bd = builder.getBeanDefinition();
//            context.getDefaultListableBeanFactory().registerBeanDefinition(method.getMethodName(), bd);
//        }
        // @Mapper bean 后置处理器
        context.registerBean(MapperPostProcessor.class);


        // 初始化容器
        context.refresh();

        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }

        context.close();
    }
}
